package mongo.db.clients.firsts;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDataFromFile {

	List<ServerAddress> seeds = null;
	MongoClient client = null;

	DB db = null;
	static DBCollection collection = null;

	public MongoDataFromFile(String ip, String db_input, String collection_input) {
		seeds = new ArrayList<ServerAddress>();
		try {
			seeds.add(new ServerAddress(ip, 27017));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

		client = new MongoClient(seeds);

		db = client.getDB(db_input);
		collection = db.getCollection(collection_input);
	}

	public static void main(String[] args) {
		
		System.out.println(Arrays.toString(args));
		
	int i = 0;	
		System.out.println("obtained inputs from command line : ip : " + args[0] + " db : " + args[1] + " collection : " + args[2]);
		
		MongoDataFromFile up = new MongoDataFromFile(args[0], args[1], args[2]);
		
		try {
			BufferedReader br = new BufferedReader(new FileReader("/tmp/data.csv"));
			String line;
			while ((line = br.readLine()) != null) {

				String[] fields = line.split(",");
				DBObject find = new BasicDBObject("_id", fields[3]);
				DBObject update1 = new BasicDBObject("$set" , new BasicDBObject("config.telnet_username", fields[5]).append("config.telnet_password", fields[6]));
				System.out.println(find + ":" + update1);
				collection.update(find, update1, true, false);
				
				System.out.println("completed : " + ++i);
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.exit(1);
		

	}

}
